/*
 * 拦截器的配置
 *
 */
import axios from 'axios';
//创建实例时设置配置的默认值
const service = axios.create({
  //baseURL: process.env.BASE_URL,
  //baseURL: "http://192.168.0.106:8888/",
  withCredentials: true,
  timeout: 60000, //请求超时的时间
});
//请求拦截器
service.interceptors.request.use((config) => {
  //console.log(location)
  return config;
}, (err) => {
  console.log(err);
  return Promise.reject(err);
});

//响应拦截器
service.interceptors.response.use((res) => {
  return res;
}, (err) => {
  console.log(err);
  if (err && err.response && err.response.status == 401) {
    //非登录 -- 路由重定向
    localStorage.setItem("userInfo", {});
    $nuxt.$store.commit('user/setUserInfo', {});
    $nuxt.$store.commit('user/setIsLogin', false);
    $nuxt.$store.commit("user/setCollectionList", []);
    $nuxt.$store.commit("menu/setCartList", []);
    //location.href = "/login?redirect=" + encodeURIComponent(location.pathname) + "&err=loginFail";
  }
  try {
    $nuxt.tips.closeToast();
  } catch (error) {

  }
  return Promise.reject(err);
});
export default service;
